﻿using Corina.Commands;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows.Input;
using System.Linq;
using System.Windows;
using System.Windows.Printing;
using Corina.Datamodel.DataObject;
using Corina.Common;
using Corina.Datamodel.Base;
namespace Corina.Lantern.Hub.Viewmodels
{
    public class SpecimenViewmodel : ViewmodelBase
    {
        public SpecimenViewmodel()
        {
            MeasureClient.GetAllSpecimenItemsCompleted += GetAllSpecimenItemsCompleted;
            MeasureClient.InsertSpecimenCompleted += InsertSpecimenCompleted;
            DeviceClient.GetAllDeviceCompleted += GetAllDeviceCompleted;
            if(DataContext.IsValid)
            {
                LoadData();
                LoadRelatedData();
            }
        }

        private void GetAllDeviceCompleted(object sender, DataService.GetAllDeviceCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                var items = (from t in e.Result.DataObjects
                             select new
                             {
                                 DeviceSn = (string)t["DeviceSn"],
                                 Kind = (string)t["Kind"]
                             });
                var newcollection = DeviceCollection.Concat(items);
                DeviceCollection = new ObservableCollection<object>(newcollection);
            }
        }

        private void InsertSpecimenCompleted(object sender, DataService.InsertSpecimenCompletedEventArgs e)
        {
            if(e.Error == null)
            {
                LoadData();
            }
        }

        private void GetAllSpecimenItemsCompleted(object sender, DataService.GetAllSpecimenItemsCompletedEventArgs e)
        {
            if(e.Error == null)
            {
                var items = (from t in e.Result.DataObjects
                             select new
                             {
                                 ItemIndex = t["ROW_NUMBER"],
                                 Id = t["Id"],
                                 DeviceSn = (string)t["DeviceSn"],
                                 Code = (string)t["Code"],
                                 Kind = t["Kind"],
                                 Name = t["Name"]
                             });
                ItemsCollection = new ObservableCollection<object>(items);
                ItemsCount = e.Result.TotalCount;
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            IsBusy = false;
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            MeasureClient.GetAllSpecimenItemsAsync(Account, Password, "", "", "", Index, 20);
        }

        protected void LoadRelatedData()
        {
            DeviceClient.GetAllDeviceAsync(Account, Password, 0, "", 1, 20);
        }

        #region Collection
        private ObservableCollection<object> _itemsCollection;
        public ObservableCollection<object> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }

        private ObservableCollection<object> _deviceCollection = new ObservableCollection<object>();
        public ObservableCollection<object> DeviceCollection
        {
            get { return _deviceCollection; }
            set
            {
                _deviceCollection = value;
                RaisePropertyChanged(() => DeviceCollection);
            }
        }
        #endregion

        #region Update
        private string _code = string.Empty;
        public string Code
        {
            get { return _code; }
            set
            {
                _code = value;
                RaisePropertyChanged(() => Code);
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        private string _name = string.Empty;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                RaisePropertyChanged(() => Name);
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        private string _deviceSn = string.Empty;
        public string DeviceSn
        {
            get { return _deviceSn; }
            set
            {
                _deviceSn = value;
                RaisePropertyChanged(() => DeviceSn);
                RaisePropertyChanged(() => SubmitCommand);
            }
        }

        private object _selectedDevice;
        public object SelectedDevice
        {
            get { return _selectedDevice; }
            set
            {
                _selectedDevice = value;
                RaisePropertyChanged(() => SelectedDevice);
                RaisePropertyChanged(() => SubmitCommand);
                DeviceSn = SelectedDevice == null ? string.Empty : GetObjectPropertyValue(SelectedDevice, "DeviceSn").ToString();
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return !string.IsNullOrEmpty(Code) && !string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(DeviceSn); },
                    ExecuteCallback = delegate {
                        MeasureClient.InsertSpecimenAsync(Account, Password, Code, Name, DeviceSn);
                    }
                };
            }
        }
        #endregion
    }
}
